<?php
//
defined('BY_JYA') or exit('error');
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$order_types = array(
		'1'=>'堂点',
		'2'=>'外卖',
		'3'=>'自取',
);
$paytypes=array('0'=>'未付款','1'=>'已付款');
if($_RQ['op']=='list'){
	$setting=$this->plugin_setting('setting');
	$setting['hdps']=json_decode($setting['hdps'],true);
	$setting['bankuai2']=json_decode($setting['bankuai2'],true);
	foreach($setting['bankuai2'] as $item){
		if($item['is_install'] == 0){
			unset($item);
		}
	}
	$setting['footernav']=json_decode($setting['footernav'],true);
	$return['setting']=$setting;
	$status = intval($_RQ['status']);
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if($_RQ['ordersn']){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=trim($_RQ['ordersn']);
	}
	if($status<10){
		$params['status']=$status;
	}else{
		$status = 10;
	}
    $fields=array('id','store_id','mid','order_type','ordersn','price','status','is_pay','pay_id','print_status','remark','createtime');
    $orderby='id desc';
	$total='';
	if($this->plugin_exists('jy_dish')){
		$list=pdo_getslice('jy_dish_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	}
	if($list){
		foreach($list as &$item){
			$item['goods']=pdo_getall('jy_dish_order_goods',array('order_id'=>$item['id']));
			$item['table']=pdo_get('jy_dish_order_table',array('order_id'=>$item['id']));
			$item['out']=pdo_get('jy_dish_order_out',array('order_id'=>$item['id']));
			$item['storename']=pdo_getcolumn('jy_dish_store',array('id'=>$item['store_id']),'title');
			$item['order_typename']=$order_types[$item['order_type']];
			$item['paytype']=$paytypes[$item['is_pay']];
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			foreach($item['goods'] as &$g){
				$g['goods_package']=$g['goods_package']?json_decode($g['goods_package'],true):array();
				if(!empty($g['goods_package'])){
					$totalprice =0;
					$goods_title = $g['goods_title'].':';
					foreach($g['goods_package'] as &$p){
						$totalprice += $p['num']*$p['price'];
						$goods_title .= '['.$p['title'].'-￥'.$p['price'].'*'.$p['num'].']';
					}
				}else{
					$goods_title = $g['goods_title'].' × '.$g['goods_num'];
					$totalprice = $g['goods_num']*$g['goods_price'];
				}
				$g['totalprice']=$totalprice;
				$g['goods_title']=$goods_title;
				$item['goods_num'] +=$g['goods_num'];
				$g['image']=pdo_getcolumn('jy_dish_goods',array('id'=>$g['goods_id']),'image');
			}
		}
	}
	$return['list']=$list;
	$return['status']=$status;
	set_html('title','订单列表');
	exi($return);
}
if($_RQ['op']=='detail'){
	if(intval($_RQ['id'])){
		$item=pdo_get('jy_dish_order',array('id'=>$_RQ['id']));
		$return['item']=$item;
		exi($return);
	}else{
		exi('订单id不存在','error','jy_dish/order/');	
	}
}
if($_RQ['op']=='cancel'){
	if($_RQ['id']){
		$item=pdo_get('jy_dish_order',array('id'=>$_RQ['id']));
		if($item['status']==0 && $item['is_pay']==1){
			$this->order_change(array('status'=>5,'order_id'=>$_RQ['id']));
			exi('取消成功','success');
		}else{
			exi('只有未确认且已支付订单可以取消','error');
		}
	}else{
		exi('无效订单','error');
	}
}
if($_RQ['op']=='receive'){
	if($_RQ['id']){
		$item=pdo_get('jy_dish_order',array('id'=>$_RQ['id']));
		$this->order_change(array('status'=>3,'order_id'=>$_RQ['id']));
		exi('确认成功','success');
	}else{
		exi('无效订单','error');
	}
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_dish_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除订单成功');
}
if($_RQ['op']=='pay'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_dish_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$params['fee']=$order['price'];
	$params['title']=$order_types[$order['order_type']].'订单';
	$params['id']=$_RQ['id'];
	$params['plugin']='jy_dish';
	$params['back_url']='jy_dish/order/payresult';
	cfc('core')->pay($params);
}
if($_RQ['op']=='refund'){
	if($_RQ['id']){
		$item=pdo_get('jy_dish_order',array('id'=>$_RQ['id']));
		if($item['is_pay']==1 && $item['status']!=5){
			$this->order_change(array('status'=>5,'order_id'=>intval($_RQ['id'])));
			exi('申请退款成功','success');
		}else{
			exi('只有已支付且未退款订单可以申请退款','error');
		}
	}
}	
?>